package com.sm.controller;

import com.sm.model.ListenEveryday;
import com.sm.util.Msg;
import com.sm.util.PageResult;
import com.sm.service.ListenService;
import com.sm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/listen")
public class ListenController {
    @Autowired
    private ListenService listenService;

    /**
     * 每日一听分页查询
     * @param pageBean
     * @return
     */
    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody PageBean pageBean) {
        PageResult pageResult = listenService.pageQuery(
                pageBean.getCurrentPage(),
                pageBean.getPageSize(),
                pageBean.getQueryString()
        );
        return pageResult;
    }

    /**
     * 每日一听详情页
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Msg selectOne(Integer id){
        if (id == null){
            return Msg.fail().add("msg","数据异常，参数为空");
        }
        ListenEveryday listenEveryday = listenService.selectOne(id);
        Integer before = listenService.getBefore(id);
        Integer after = listenService.getAfter(id);
        if (ObjectUtils.isEmpty(listenEveryday)){
            return Msg.success().add("msg","暂无数据");
        }
        return Msg.success().add("listenEveryday",listenEveryday).add("before",before).add("after",after);
    }

    /**
     * 首页--每日一听列表
     */
    @RequestMapping("/listenList")
    @ResponseBody
    public Msg listenList(Integer currentPage,Integer pageSize){
        PageResult pageResult = listenService.pageQuery(currentPage, pageSize, null);
        if (ObjectUtils.isEmpty(pageResult)){
            return Msg.fail().add("msg","暂无数据");
        }
        return Msg.success(pageResult);
    }
}
